/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.institution.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.institution.service.api.CategoryService;
import com.koron.institution.uitls.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "Category", description = "制度法规目录Api类")
@RestController
@RequestMapping(value = "/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperation("制度目录列表")
    @RequestMapping(value = "/categoryList.htm", method = {RequestMethod.GET})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpCode", value = "公司code", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "institutionType", value = "法规制度类型(1.集团制度，2.公司制度，3厂站所制度)", required = true, paramType = "query", dataType = "int")
    })
    public MessageBean<?> categoryList(String corpCode, Integer institutionType, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null == user) {
            return MessageBean.create(BusinessCode.NOT_LONGIN.getCode(), BusinessCode.NOT_LONGIN.getDescription(), void.class);
        }
        return categoryService.getCategoryLists(corpCode, user, institutionType);
    }


    @ApiOperation("新增制度目录")
    @RequestMapping(value = "/categoryAdd.htm", method = {RequestMethod.POST})
    @Log(title = "新增制度目录", businessModule = "制度法规管理", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> categoryAdd(@RequestParam String institutionCategoryStr, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String institutionStr, String corpCode, MultipartFile[] file) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        return categoryService.addCategory(institutionCategoryStr, user, institutionStr, corpCode,file);
    }

    @ApiOperation("删除制度目录")
    @RequestMapping(value = "/categoryDelete.htm", method = {RequestMethod.POST})
    @ApiImplicitParam(name = "categoryId", value = "目录id", required = true, paramType = "query", dataType = "String")
    @Log(title = "删除制度目录", businessModule = "制度法规管理", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> categoryDelete(String categoryId, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        return categoryService.delete(categoryId, user);
    }

    @ApiOperation("修改制度目录")
    @RequestMapping(value = "/categoryUpdate.htm", method = {RequestMethod.POST})
//    @ApiImplicitParam(name = "categoryId", value = "目录id", required = true, paramType = "query", dataType = "String")
    @Log(title = "修改制度目录",businessModule = "制度法规管理",  businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> categoryUpdate(@RequestParam String institutionCategoryStr, @SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String institutionId, String corpCode, Boolean flag, MultipartFile[] file,
                                         @RequestParam(required = false) final String keepFiles,
                                         @RequestParam(required = false) final String keepPDFFiles,
                                         @RequestParam(required = false) final String delFiles,
                                         @RequestParam(required = false) final String delPDFFiles
    ) {
        if (null == user) {
            MessageBean<?> msg = new MessageBean<>();
            msg.setCode(BusinessCode.NOT_LONGIN.getCode());
            msg.setDescription(BusinessCode.NOT_LONGIN.getDescription());
            return msg;
        }
        if (null == flag) {
            flag = false;
        }
        return categoryService.updateCategory(institutionCategoryStr, user, flag, institutionId,corpCode, file,keepFiles,keepPDFFiles,delFiles,delPDFFiles);
    }

}
